﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Credit Card Type Controller
    /// </summary>
    [Authorize]
    public class CreditCardTypeController : Controller
    {
        #region Fields
        private const int DEFAULTPAGESIZE = 10;
        readonly ICreditCardTypeRepository _creditCardTypeRepository;

        #endregion

        #region Constructors
        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public CreditCardTypeController() : this(new CreditCardTypeRepository()) { }

        /// <summary>
        /// Credit Card Type Controller Constructor
        /// </summary>
        /// <param name="customerCreditCardTypeRepository"></param>
        public CreditCardTypeController(ICreditCardTypeRepository customerCreditCardTypeRepository)
        {
            _creditCardTypeRepository = customerCreditCardTypeRepository;
        }
        #endregion

        #region Actions
        /// <summary>
        /// Display the listing of Credit Card Type
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<CreditCardType> customerCreditCardType = !string.IsNullOrWhiteSpace(strQuery) ? _creditCardTypeRepository.FindCreditCardTypeByName(strQuery).OrderBy(d => d.CreatedDate) : _creditCardTypeRepository.FindAllCreditCardType().OrderBy(d => d.CreatedDate);
            var paginatedCreditCardType = customerCreditCardType.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedCreditCardType);
        }

        /// <summary>
        /// Details of Credit Card Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var customerCreditCardType = _creditCardTypeRepository.GetCreditCardTypeById(id.Value);

                if (customerCreditCardType == null)
                {
                    return new FileNotFoundResult { Message = "No CreditCardType found for that id" };
                }

                return View(new CreditCardTypeViewModel(customerCreditCardType));
            }

            return new FileNotFoundResult { Message = "No CreditCardType found due to invalid CreditCardType id" };
        }

        /// <summary>
        /// Create Credit Card Type
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var creditCardType = new CreditCardType();
            return View(new CreditCardTypeViewModel(creditCardType));
        }

        /// <summary>
        /// Create Credit Card Type
        /// </summary>
        /// <param name="creditCardType"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(CreditCardType creditCardType)
        {
            if (ModelState.IsValid)
            {
                creditCardType.UserCreated = HttpContext.User.Identity.Name;
                creditCardType.CreatedDate = DateTime.Now.ToSqlDateTime();
                creditCardType.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _creditCardTypeRepository.Add(creditCardType);
                _creditCardTypeRepository.Save();
                return RedirectToAction("Details", new { id = creditCardType.CreditCardTypeId });
            }

            return View(new CreditCardTypeViewModel(creditCardType));
        }

        /// <summary>
        /// Edit Credit Card Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var creditCardType = _creditCardTypeRepository.GetCreditCardTypeById(id);
            return View(new CreditCardTypeViewModel(creditCardType));
        }

        /// <summary>
        /// Edit Credit Card Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var creditCardType = _creditCardTypeRepository.GetCreditCardTypeById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    creditCardType.UserModified = HttpContext.User.Identity.Name;
                    creditCardType.ModifiedDate = DateTime.Now;
                    UpdateModel(creditCardType, "CreditCardType");
                    _creditCardTypeRepository.Save();
                    return RedirectToAction("Details", new { id = creditCardType.CreditCardTypeId });
                }
                catch
                {
                    return View(new CreditCardTypeViewModel(creditCardType));
                }
            }
            return View(new CreditCardTypeViewModel(creditCardType));
        }

        /// <summary>
        /// Delete Credit Card Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var customerCreditCardType = _creditCardTypeRepository.GetCreditCardTypeById(id);
            return customerCreditCardType == null ? View("NotFound") : View(new CreditCardTypeViewModel(customerCreditCardType));
        }

        /// <summary>
        /// Delete Credit Card Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var customerCreditCardType = _creditCardTypeRepository.GetCreditCardTypeById(id);

            if (customerCreditCardType != null)
            {
                _creditCardTypeRepository.Delete(customerCreditCardType);
                _creditCardTypeRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }
        #endregion
    }
}